import {emailList} from "../parseEmail/parse_fun";
import md5 from "../../../../assets/js/md5";

/**
 * 移除黑名单-确认框
 * @param self
 * @param tiyData
 */
export const deleted = (self, tiyData,index) => {
  tiyData.tStatus.modalDelete.email.id = tiyData.tData.blacklist[index].id;
  tiyData.tStatus.modalDelete.email.userEmail = tiyData.tData.blacklist[index].userEmail;
  if(tiyData.tData.blacklist[index].type==1){
    tiyData.tStatus.modalDelete.title="用户";
  }
  if(tiyData.tData.blacklist[index].type==2){
    tiyData.tStatus.modalDelete.title="域名";
  }
  tiyData.tStatus.modalDelete.isShow = true;
};

/**
 * 移除黑名单
 * @param self
 * @param tiyData
 */
export const webDelete = function (self, tiyData) {
  tiyData.tStatus.modalDelete.isLoading = true;
  let params={
    id: tiyData.tStatus.modalDelete.email.id,
  }
  self.$apiEmail.deleteBlacklist(params)
    .then(function (response) {
      tiyData.tStatus.modalDelete.isShow = false;
      changeModelLoading(self, tiyData);
      if (response.success) {
        webList(self, tiyData)
        self.$Message.success('删除成功!');
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};

//添加黑名单
export const addItem = function (self, tiyData, name) {
  tiyData.tStatus.modalAdd.isLoading = true;
  self.$refs[name].validate((valid) => {
    if (valid) {
      let params = {
        userEmail: tiyData.tData.email.type==1?tiyData.tData.email.userEmail:tiyData.tData.email.domain,
        type:tiyData.tData.email.type,
        reason: tiyData.tData.email.reason,
      };
      self.$apiEmail.addBlacklist(params).then(response => {
        changeModelLoading(self, tiyData);
        if (response.success) {
          self.$Message.success("新增成功!");
          tiyData.tStatus.modalAdd.isShow = false;
          self.search();
        } else {
          self.$Message.error(response.errorMessage);
        }
      });
    }else {
      changeModelLoading(self, tiyData);
    }
  });
};

//查询黑名单
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$ySpliceAll(tiyData.tData.blacklist);

  let param = {
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "userName": tiyData.tParam.userName.trim(),
    "userEmail": tiyData.tParam.userEmail.trim(),
  };

  self.$apiEmail.queryBlacklist(param).then(function (response) {
    if(response.success){
      let emailPage = response.result;
      tiyData.tPage.total = emailPage.total;
      tiyData.tPage.stepNumber = emailPage.pageSize;
      emailPage.list.forEach(
        function (item) {
          tiyData.tData.blacklist.push(item);
        }
      );
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
};


function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalDelete.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDelete.isLoading = true;
  });

  tiyData.tStatus.modalAdd.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalAdd.isLoading = true;
  });
}
